//
//  ESLoginVC.m
//  Eyeshot
//
//  Created by admin on 15/7/12.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESLoginVC.h"
#import "ESRegisterVC.h"

@interface ESLoginVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userName;

@property (weak, nonatomic) IBOutlet UITextField *passwd;

@end

@implementation ESLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _userName.delegate = self;
    _passwd.delegate = self;
    _passwd.secureTextEntry = YES;
    _userName.tintColor = [UIColor whiteColor];
    _passwd.tintColor = [UIColor whiteColor];

    _userName.attributedPlaceholder = [self attributedString:@"用户名" fontOfSize:16 color:[UIColor grayColor]];
    _passwd.attributedPlaceholder = [self attributedString:@"密码" fontOfSize:16 color:[UIColor grayColor]];
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - delegate


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.attributedPlaceholder = [self attributedString:textField.placeholder fontOfSize:16 color:[UIColor whiteColor]];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.attributedPlaceholder = [self attributedString:textField.placeholder fontOfSize:16 color:[UIColor grayColor]];
}
                                       
#pragma mark - UI Action

- (IBAction)cancel:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)register:(UIButton *)sender {
    ESRegisterVC *regVc = [[ESRegisterVC alloc] init];
    [self.navigationController pushViewController:regVc animated:YES];
}

- (IBAction)login:(UIButton *)sender {
    [BmobUser loginInbackgroundWithAccount:_userName.text andPassword:_passwd.text block:^(BmobUser *user, NSError *error) {
        
        if (user) {
            [self dismissViewControllerAnimated:YES completion:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UserOK" object:nil userInfo:nil];
        } else {
            ESLog(@"%@",error);
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:@"用户名密码输入错误" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确 认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//                NSLog(@"%s %d",__FUNCTION__, __LINE__);
                _userName.text = nil;
                _passwd.text = nil;
                [_userName becomeFirstResponder];
                
            }];
            [alertC addAction:action];
            [self presentViewController:alertC animated:YES completion:^{
//                NSLog(@"%s %d",__FUNCTION__, __LINE__);
                
            }];
        }
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - other
- (NSAttributedString *) attributedString:(NSString *)string fontOfSize:(NSInteger)size color:(UIColor *)color {
    NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:size],NSForegroundColorAttributeName :color};
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string attributes:dict];
    return attrString;
}
@end
